/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.AppFuelReceivePageDTO;
import com.pig4cloud.pigx.user.dto.FuelConsumePageDTO;
import com.pig4cloud.pigx.user.entity.FuelConsume;
import com.pig4cloud.pigx.user.service.FuelConsumeService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.LoginUserFuelConsumeVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 燃料消耗记录
 *
 * @author pigx code generator
 * @date 2022-11-24 17:37:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fuel_consume" )
@Api(value = "fuel_consume", tags = "燃料消耗记录管理")
public class FuelConsumeController {

    private final  FuelConsumeService fuelConsumeService;

	/**
	 * 获取策略筛选选项
	 * @return r
	 */
	@GetMapping("/strategy/option")
	public R<List<String>> getStrategyNameOption() {
		try {
			List<FuelConsume> consumes = fuelConsumeService.list(new QueryWrapper<FuelConsume>() {{
				select("strategy_name");
				groupBy("strategy_name");
			}});
			if (consumes.size() > 0) {
				List<String> strings = consumes.stream().map(FuelConsume::getStrategyName).collect(Collectors.toList());
				return R.ok(strings);
			}
			return R.ok(new ArrayList<>());
		} catch (Exception e) {
			return R.ok(new ArrayList<>());
		}
	}

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    public R<AdminPageDemoVO<FuelConsume>> getFuelConsumePage(@RequestBody FuelConsumePageDTO dto) {
        return fuelConsumeService.pageList(dto);
    }

	/**
	 * 获取用户燃料消耗记录
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/app/page")
	public R<Page<LoginUserFuelConsumeVO>> getAppPage(@RequestBody AppFuelReceivePageDTO dto) {
		return fuelConsumeService.getAppPage(dto);
	}


    /**
     * 通过id查询燃料消耗记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_fuelconsume_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fuelConsumeService.getById(id));
    }

    /**
     * 新增燃料消耗记录
     * @param fuelConsume 燃料消耗记录
     * @return R
     */
    @ApiOperation(value = "新增燃料消耗记录", notes = "新增燃料消耗记录")
    @SysLog("新增燃料消耗记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_fuelconsume_add')" )
    public R save(@RequestBody FuelConsume fuelConsume) {
        return R.ok(fuelConsumeService.save(fuelConsume));
    }

    /**
     * 修改燃料消耗记录
     * @param fuelConsume 燃料消耗记录
     * @return R
     */
    @ApiOperation(value = "修改燃料消耗记录", notes = "修改燃料消耗记录")
    @SysLog("修改燃料消耗记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_fuelconsume_edit')" )
    public R updateById(@RequestBody FuelConsume fuelConsume) {
        return R.ok(fuelConsumeService.updateById(fuelConsume));
    }

    /**
     * 通过id删除燃料消耗记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除燃料消耗记录", notes = "通过id删除燃料消耗记录")
    @SysLog("通过id删除燃料消耗记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_fuelconsume_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fuelConsumeService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param fuelConsume 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_fuelconsume_export')" )
    public List<FuelConsume> export(FuelConsume fuelConsume) {
        return fuelConsumeService.list(Wrappers.query(fuelConsume));
    }
}
